﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace ProjectExercise
{
    public partial class DatePicker : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                DateTime time = DateTime.Now;
                string format = "d";
                string dateNow = time.ToString(format);
                string[] splitDate = dateNow.Split('/');
                string month = splitDate[0];
                string date = splitDate[1];
                string year = splitDate[2];
                int dateCount = DateTime.DaysInMonth(Int32.Parse(year), Int32.Parse(month));

                for (int i = 1; i <= dateCount; i++)
                {
                    DateCombo.Items.Add(new ListItem(i.ToString(), i.ToString()));
                }

                for (int i = 1900; i <= Int32.Parse(year) + 100; i++)
                {
                    YearCombo.Items.Add(new ListItem(i.ToString(),i.ToString()));
                }

                DateCombo.Text = date;
                MonthCombo.Text = month;
                YearCombo.Text = year;
            }      
        }

        protected void MonthCombo_SelectedIndexChanged(object sender, EventArgs e)
        {        
            int dateCount = DateTime.DaysInMonth(Int32.Parse(YearCombo.SelectedValue.ToString()), Int32.Parse(MonthCombo.SelectedValue.ToString()));
            for (int i = 0; i < dateCount; i++)
            {
                DateCombo.Items.Clear();
            }
            for (int j = 1; j <= dateCount; j++)
            {
                DateCombo.Items.Add(new ListItem(j.ToString(), j.ToString()));
            }
        }
    }
}